using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace Elevation.Common.Services.Helpers
{
	internal sealed class SafeProcessHandle : SafeHandleZeroOrMinusOneIsInvalid
	{
		#region Extern

		[DllImport("kernel32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		private static extern bool CloseHandle(IntPtr handle);

		#endregion Extern

		#region Constructors

		private SafeProcessHandle()
			: base(true)
		{
			//
		}

		#endregion Constructors

		#region SafeHandle overrides

		protected override bool ReleaseHandle()
		{
			try
			{
				return CloseHandle(handle);
			}
			catch
			{
				return false;
			}
		}

		#endregion SafeHandle overrides
	}
}